package com.beans;

/**
 * Created by geange on 16-12-16.
 */
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import java.util.ArrayList;
import com.beans.BookBean;
public class BookDAO{

    String driverName = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/learnDB";
    String userName = "root";
    String passWord = "xxxxxx";

    private static InitialContext context= null;
    private DataSource dataSource = null;
    public BookDAO(){
        try{
            if(context == null){
                context = new InitialContext();
            }
            dataSource = (DataSource)context.lookup("java:comp/env/jdbc/MySQL");
        }catch(NamingException e2){
        }
    }
    // 根据书号查询图书信息
    public BookBean searchBook(String bookid){
        Connection conn = null;
        PreparedStatement pstmt = null;

        Statement stmt = null;

        ResultSet rst = null;
        BookBean book = new BookBean();
        try{
//            conn = dataSource.getConnection();
            conn = DriverManager.getConnection(url, userName, passWord);

//            pstmt = conn.prepareStatement("SELECT * FROM books WHERE bookid=?");
//            System.err.println(bookid);
//            pstmt.setString(1,bookid);
//            rst = pstmt.executeQuery();
//            System.err.println(rst.getString(0));

            stmt = conn.createStatement();
            String SQL = "SELECT * FROM books WHERE bookid=\""+bookid+"\"";
            rst = stmt.executeQuery(SQL);

            if(rst.next()){
                System.err.println(rst.getString("bookid"));
                book.setBookid(rst.getString("bookid"));
                book.setTitle(rst.getString("title"));
                book.setAuthor(rst.getString("author"));
                book.setPublisher(rst.getString("publisher"));
                book.setPrice(rst.getFloat("price"));
                return book;
            }else{
                return null;
            }
        }catch(SQLException se){return null;
        }finally{
            try{
                conn.close();
            }catch(SQLException se){
            }
        }
    }
    // 插入一本图书记录
    public boolean insertBook(BookBean book){
        Connection conn = null;
        PreparedStatement pstmt = null;
        Statement stmt = null;
        try{
//            conn = dataSource.getConnection();
            conn = DriverManager.getConnection(url, userName, passWord);
            System.err.println(book.getBookid()+" "+book.getTitle()+" "+book.getAuthor()+" "+book.getPublisher()+" "+book.getPrice());
//            pstmt = conn.prepareStatement(
//                    "INSERT INTO books VALUES(?,?,?,?,?)");
//            pstmt.setString(1,book.getBookid());
//            pstmt.setString(2,book.getTitle());
//            pstmt.setString(3,book.getAuthor());
//            pstmt.setString(4,book.getPublisher());
//            pstmt.setFloat(3,book.getPrice());
//            System.err.println(pstmt.getGeneratedKeys());
//            pstmt.executeUpdate();
//            pstmt.close();
            stmt = conn.createStatement();
            String strExe = "INSERT INTO books VALUES("
                    +"'"+book.getBookid()+"',"
                    +"'"+book.getTitle()+"',"
                    +"'"+book.getAuthor()+"',"
                    +"'"+book.getPublisher()+"',"
                    +book.getPrice()+")";
            stmt.execute(strExe);
            return true;
        }catch(SQLException se){
            return false;
        }finally{
            try{
                conn.close();
            }catch(SQLException se){ }
        }
    }
}
